package src;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class JTextAreaWithBackground extends JTextArea
{
    Rectangle bounds = super.getBounds();
    Color o = Color.BLUE;
    Color t = Color.white;
    Point first = new Point( 0 , 0 );
    Point second = new Point( 0 , 0 );
    
    GradientPaint gradient = null;

    public JTextAreaWithBackground()
    {
    }

    @Override
    public void paint( Graphics g )
    {
        Graphics2D g2d = ( Graphics2D ) g;

        g2d.setPaint( gradient );
        g2d.fill( bounds );

        super.paint( g );
    }

    public void setColors( Color one, Color two )
    {
        o = one;
        t = two;

        gradient = new GradientPaint( new Point ( 0, 50), this.o, new Point(50, 600), this.t, false );

        repaint();
    }

    public void setBounds( Rectangle bounds )
    {
        this.bounds = bounds;
    }

    public void setTransition( int p1, int p2, int p3, int p4 )
    {

        setOpaque( false );

        Point lastFirst = first;
        Point lastSecond = second;

        first = new Point ( p1, p2 );
        second = new Point ( p3, p4 );

        SetColor setColor = new SetColor( lastFirst, lastSecond, first, second, o, t );

        if ( setColor.isAlive() )
        {
            setColor.finishQuickly();
            setColor.run();
        }
        else
        {
            setColor.run();
        }
       
    }

    class SetColor extends Thread
    {
        Point lastFirst;
        Point lastSecond;
        Point first;
        Point second;

        Point newSecondPosition;

        Color o;
        Color t;

        long timeToWait = 5;

        boolean finish = false;
        boolean finishQ = false;

        public SetColor( Point lastFirst, Point lastSecond, Point first, Point second, Color o, Color t )
        {
            this.lastFirst = lastFirst;
            this.lastSecond = lastSecond;
            this.second = second;
            this.first = first;

            this.o = o;
            this.t = t;

            finish = false;

            newSecondPosition = lastSecond;


        }

        @Override
        public void run()
        {
            while ( ! finish )
            {
                int t = 10;

                Degub.getInstance().Debug("TEXTURE PROCESS " +second.x + " starting process texture name: " + this.getName() );

                for( int x  = 0; x < second.x; x+=t )
                {
                    newSecondPosition.x += t;
                    gradient = new GradientPaint( this.first, this.o, this.newSecondPosition, this.t, false );
                    
                    wait( 1 );
                    
                    repaint();

                    t = ( finishQ ) ? ( newSecondPosition.x - t ) / 5 : t;
                }

                finish = true;
                finishQ = false;
            }
        }

        public void wait( int n )
        {
            long t0, t1;
            t0 = System.currentTimeMillis();
            do
            {
                t1 = System.currentTimeMillis();
            }
            while ( t1 - t0 < 50 );
        }


        public void finishQuickly()
        {
            finishQ = true;
        }
    }

    public static void wait( int n )
    {
        long t0, t1;
        t0 = System.currentTimeMillis();
        do
        {
            t1 = System.currentTimeMillis();
        }
        while ( t1 - t0 < n );
    }

    public static void main( String args[] )
    {
        JFrame f = new JFrame();
        final JTextAreaWithBackground box = new JTextAreaWithBackground();
        JButton b = new JButton("b");
        JButton c = new JButton("c");
        JButton d = new JButton("d");

        b.setSize( 20, 20 );

        f.setLayout( new GridLayout() );
        f.add( box );
        box.setSize( 200, 130 );
        f.add( b );

        f.setSize( 500, 300 );
        f.setVisible( true );

        box.setBounds( box.getVisibleRect() );
        box.setColors( new Color( 150, 230, 230 ), Color.white );

        b.addMouseListener( new MouseListener() {

            public void mouseClicked( MouseEvent e )
            {
                box.setTransition( 0, 0, box.getSize().width, 0 );
            }

            public void mousePressed( MouseEvent e )
            {
            }

            public void mouseReleased( MouseEvent e )
            {
            }

            public void mouseEntered( MouseEvent e )
            {
            }

            public void mouseExited( MouseEvent e )
            {
            }
        });
                
         
//
//        c.addItemListener( new ItemListener() {
//
//            public void itemStateChanged( ItemEvent e )
//            {
//                box.setTransition( 0, 0, box.getBounds().x/2, 0 );
//            }
//        });
//
//        d.addItemListener( new ItemListener() {
//
//            public void itemStateChanged( ItemEvent e )
//            {
//                box.setTransition( 0, 0, 0, 0 );
//            }
//        });
    }

}